# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import pymysql
import pymongo


class ItblogPipeline:

    def __init__(self, host, port, user, password, db):
        self.conn = pymysql.connect(host=host,user=user,port=port,
                        password=password,database=db)
        self.cursor = self.conn.cursor()

    @classmethod
    def from_crawler(cls, crawler):
        host = crawler.settings["HOST"]
        port = crawler.settings["PORT"]
        user = crawler.settings["USER"]
        password = crawler.settings["PASSWORD"]
        db = crawler.settings["DB"]

        return cls(host, port, user, password, db)

    def process_item(self, item, spider):

        # print(item)
        # 编写添加数据的sql语句
        add_sql = "insert into blog(title,author,detail_url,description,publish_time,content) value(%s,%s,%s,%s,%s,%s)"
        self.cursor.execute(add_sql,(item["title"], item["author"], item["detail_url"], item["description"],
                                     item["publish_time"], item["content"]))
        self.conn.commit()  # 提交数据
        return item


    def __del__(self):
        self.conn.close()



class MongoPipeline:

    def __init__(self, host, port, db):
        conn = pymongo.MongoClient(host, port)
        db = conn[db]
        self.collection = db["it_blog"]



    @classmethod
    def from_crawler(cls, crawler):
        host = crawler.settings["MONGO_HOST"]
        port = crawler.settings["MONGO_PORT"]
        db = crawler.settings["MONGO_DB"]

        return cls(host, port, db)


    def process_item(self, item, spider):
        document = ItemAdapter(item).asdict()
        self.collection.insert_one(document)

        return item


class FilePipeline:

    def process_item(self, item, spider):
        return item


